define(
    function (require) {
        require('tpl!./list.tpl.html');
        var util = require('common/util');
        var globalData = require('common/GlobalData').getInstance();
        var u = require('underscore');

        var showid = 1;
        var page =1 ;

        var fields = [
            {
                title: '编号',
                field: 'id' ,
                sortable: true,
                resizable: true,
                width: 20,
                content: function (item) {
                    return showid++;
                }
            },
            {
                title: '手机号',
                field: 'phone' ,
                resizable : true,
                width: 100,
                content: function (item) {
                    return '<span class="list-link" data-command-args="' + item.id + '" data-command="modify">' + item.phone + '</a>';
                }
            },
            {
                title: '券号',
                field: 'number' ,
                width: 100,
                content: function (item) {
                    return item.number;
                }
            },
            {
                title: '状态',
                field: 'status' ,
                width: 60,
                content: function (item) {
                    return textNb(item.status);
                }
            },
            {
                title: '有效开始时间',
                field: 'svtime' ,
                width: 130,
                content: function (item) {
                    return textTime(item.svtime);
                }
            },
            {
                title: '有效结束时间',
                field: 'evtime' ,
                width: 130,
                content: function (item) {
                    return textTime(item.evtime);
                }
            },
            {
                title: '转送',
                field: 'transflg' ,
                width: 80,
                content: function (item) {
                    if(item.transflg===0){
                        return "未转送";
                    }
                    return "已转送";
                }
            },
            {
                title: '能否转送',
                field: 'sure_transflg' ,
                width: 100,
                content: function (item) {
                    if(item.sureTransflg===0){
                        return "能";
                    }
                    return "不能";
                }
            },
            {
                title: '转送父ID',
                field: 'parentid' ,
                width: 50,
                content: function (item) {
                    return item.parentid;
                }
            },
            {
                title: '领取状态',
                field: 'reciveflg' ,
                width: 130,
                content: function (item) {
                    if(item.reciveflg===0){
                        return "未领取";
                    }
                    return "已领取";
                }
            },
            {
                title: '操作',
                field: 'operate' ,
                width: 60,
                content: function (item) {
                    if(item.deleteflg===0 && item.status===0 ){
                        var operates = [
                            '<span class="list-link" data-command-args="'
                            + item.id
                            + '" data-command="delete">删除</a></span>'
                        ];
                        return operates.join('');
                    }else{

                    }

                }
            }
        ];

        function search(e) {
            var phone = this.get('phone');
            if(phone.getValue().length>10){
                this.fire('search', { phone: phone.getValue(),page:page});
            }else{
                alert("请输入正确格式的电话号码！");
            }
        }
        //分页
        function flip(e) {
            var page = e.target.getAttribute('data-page');
            if (/(\+|\-)/.test(page)) {
                page = parseInt(page) + (RegExp.$1 === '+' ? 1 : -1);
            }
            var  phone = this.get('phone').getValue();

            if(phone.length>0){
                this.fire('search', { phone: phone,page:page});
            }else {
                this.fire('flip', { page: page });
            }
        }

        //时间戳转换
        function textTime(obj) {
            if(obj !== null){
                var seperator1 = "-";
                var year=obj.substring(0,4);
                var month=obj.substring(4,6);
                var dd=obj.substring(6,8);
                var currentdate=year+seperator1+month+seperator1+dd;
                return currentdate;
            }
             return "--";
        }

        function  textNb(obj) {
            if(obj === 1){
                return "已使用";
            }else if(obj === 2){
                return "已过期";
            }
            return "未使用";
        }


        var BaseListView = require('common/ListView');

        function ExperienceTicketListView() {
            BaseListView.apply(this, arguments);
        }

        ExperienceTicketListView.prototype.template = 'experienceTicketListPage';

        ExperienceTicketListView.prototype.getUIProperties = function () {
            BaseListView.prototype.getUIProperties.apply(this, arguments);
            var str = location.href;
            var num = str.lastIndexOf('=');
            if(num === -1){
                page = 1;
            }else {
                var name = str.substring(num-4, num);
                if(name === 'page'){
                    page =  str.substring(num + 1, str.length);
                }else {
                    page = 1;
                }
            }
            showid = (page-1)*10+1;
            return {
                list: {
                    fields: fields,
                    sortable: false,
                    columnResizable: true,
                    subrow: false,
                    followHead: true,
                    selectMode: 'line',
                    noDataHtml: '<p style="margin:0;">暂无电子券信息!</p>'
                }
            };
        }


        ExperienceTicketListView.prototype.enterDocument = function () {
            BaseListView.prototype.enterDocument.apply(this, arguments);

            var me = this;
            this.get('list').on(
                'command',
                function (event) {
                    me.fire(event.name, event);
                }
            );

            this.get('createButton').on(
                'click',
                this.fire.bind(this, 'create')
            );

            this.get('searchButton').on(
                'click',
                util.bind(search, this)
            );
            $('#list-page').on(
                'click',
                ':not(.disable)' ,
                util.bind(flip, this)
            );
            this.get('list').initChildren();
        }

        require('er/util').inherits(ExperienceTicketListView, BaseListView);
        return ExperienceTicketListView;
    }
);
